#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
@todo
@Author : sunp
@Date :2021/11/21
'''

from com.dtmilano.android.viewclient import ViewClient
from com.dtmilano.android.viewclient import ViewNotFoundException
from com.dtmilano.android.adb.adbclient import AdbClient
from src.reader.welfare.welfare import Welfare
from datetime import datetime
from src.reader.bookshelf.bookshelf import Bookself
import time


class App:

    def __init__(self):
        device, serialno = ViewClient.connectToDeviceOrExit()
        viewClient = ViewClient(device=device, serialno=serialno)
        self.device = device
        self.serialno = serialno
        self.vc = viewClient


    def openApp(self):
        # 不管在何时 间隔点击两次home 保证 home 处于 第一页 (app在第二页)
        self.device.press('KEYCODE_HOME')
        time.sleep(2)
        self.device.press('KEYCODE_HOME')
        adb = AdbClient(serialno = self.serialno)
        # db.shell(_cmd = 'pm list packages')
        _cmd = 'input swipe %d %d %d %d' % (1000, 1800, 200, 1800)
        adb.shell(_cmd=_cmd)
        self.vc.dump()
        # not found with ViewNotFoundException
        adb.shell(' am force-stop com.dragon.read ') # kill 番茄小说 进程
        self.vc.findViewWithTextOrRaise(u'番茄免费小说').touch()
        print(u'正在打开番茄免费小说App,请稍等...')
        time.sleep(10) # 等待app开启时广告

    '''签到'''
    def signIN(self):
        try:
            self.openApp()
            self.vc.dump(5)
            # 关闭签到弹出框
            self.vc.touch(540, 1600)
        except ViewNotFoundException:
            # 如果出错，继续向下执行
            print(u'立即签到 not found, current time is {0}'.format(datetime.now()))

    def openHome(self):
        # viewClient = ConnectDevice.openViewClient()
        pass

    def openPage(self):
        pass


    '''福利'''
    def openWelfare(self) :
        self.vc.dump(5)
        self.vc.findViewWithText(u'福利').touch()
        print(u'正在打开福利...')
        # 休眠5s，等待页面加载
        time.sleep(5)
        self.vc.dump(5)



    '''早晚9点睡觉'''
    def sleep(self):
        print(f'番茄免费阅读小说->python自动化睡觉 was started at %s ' % datetime.now())
        self.openApp()
        self.openWelfare()
        self.vc.swipe(startX=500, startY=2000, endX=500, endY=1000)
        welfare = Welfare(self.vc)
        welfare.openSleep()
        self.vc.swipe(startX=500, startY=2000, endX=500, endY=-1)
        time.sleep(5)
        welfare.openBox()

    '''8:00/12:00/18:00吃饭'''
    def eat(self):
        print(f'番茄免费阅读小说->python自动化吃饭 was started at %s ' % datetime.now())
        self.openApp()
        self.openWelfare()
        # 滑动到底部
        self.vc.swipe(startX=500, startY=2000, endX=500, endY=-1)
        time.sleep(5)
        welfare = Welfare(self.vc)
        welfare.openBox()
        welfare.openEat()


    '''书架'''
    def openBoolshelf(self):
        time.sleep(3)
        self.vc.dump()
        self.vc.findViewWithText(u'书架').touch()
        print(u'正在打开书架...')
        time.sleep(10)


    '''我的'''
    def openMine(self):
        pass


    '''阅读'''
    def read(self):
        print(f'番茄免费阅读小说->python自动化阅读 was started at %s '% datetime.now())
        self.openApp()
        self.openBoolshelf()
        book = Bookself(self.vc)
        book.openBookself()
